﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace OverlakeApp
{
    /// <summary>
    /// The main application page.
    /// </summary>
    public partial class MainPage : PhoneApplicationPage
    {
        ViewModels.OverlakeViewModel vm;

        /// <summary>
        /// The constructor for the main page.
        /// </summary>
        public MainPage()
        {
            InitializeComponent();

            this.vm = this.DataContext as ViewModels.OverlakeViewModel;
            vm.MessageBoxWrapper = (a) => System.Windows.MessageBox.Show(a);
            vm.MessageBoxWrapper2 = (a, b, c) => System.Windows.MessageBox.Show(a, b, c);
            //App.Dispatcher = this.Dispatcher;
            ViewModels.ViewModelBase.Dispatcher = this.Dispatcher;
        }

        /// <summary>
        /// Called when the page becomes active in a frame.  Currently just deselects the teacher.
        /// </summary>
        /// <param name="e">An object that contains the event data.</param>
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            this.vm.SelectedTeacher = null;
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0)
                return;
            else
                NavigationService.Navigate(new Uri("/Views/ViewTeacherDetail.xaml", UriKind.Relative));
        }

        private void StackPanel_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/ViewLunchesPage.xaml", UriKind.Relative));
        }

        private void cal_SelectionChanged(object sender, WPControls.SelectionChangedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/ViewCalendarDayPage.xaml", UriKind.Relative));
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!Settings.ContainsKey("FirstBoot"))
            {
                Settings.AddOrUpdateValue("FirstBoot", false);
                //Settings.Save();
                if (global::System.Windows.MessageBox.Show("Looks like you're new around here! You will need to log into your Overlake account to access teachers, and your schedule.  Would you like to do this now?  If not, you can log in later in settings.", "Welcome!", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    NavigationService.Navigate(new Uri("/Views/SettingsPage.xaml", UriKind.Relative));
                }
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if(vm.RefreshCommand.CanExecute(null))
                vm.RefreshCommand.Execute(null);
        }

        private void btnSettings_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/SettingsPage.xaml", UriKind.Relative));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/ViewSchedulePage.xaml", UriKind.Relative));
        }

        private void ListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                string id = (e.AddedItems[0] as AppService.Section).SectionID;
                vm.CurrentSection = vm.Sections.First(s => s.SectionID == id);
                NavigationService.Navigate(new Uri("/Views/ViewCoursePage.xaml", UriKind.Relative));

                (sender as ListBox).SelectedItem = null;
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/ViewLunchesPage.xaml", UriKind.Relative));
        }
    }
}